package com.maxima.attendance.actions;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.Result;

import com.google.gson.Gson;
import com.maxima.attendance.constants.Constants;
import com.maxima.attendance.daos.HolidayDAO;
import com.maxima.attendance.pojos.Holiday;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class HolidaysAction extends ActionSupport {
	private static final long serialVersionUID = 1L;
	private static Integer id;
	private String jsonResponse = "";
	private static Logger logger = Logger.getLogger(ProjectAction.class.getName());
	private HolidayDAO holidayDAO = new HolidayDAO();
	private SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");

	@Override
	public String execute() throws Exception {
		return SUCCESS;
	}

	@Action(value = "addHoliday", results = {
	@Result(name = "success", type = "json"), @Result(name = "error", type = "json")
	}, interceptorRefs = {
		@InterceptorRef(value = "adminStack")
	})
	public String addHoliday() {
		Date date = getDate();
		if (holidayDAO.checkIfAlreadyPresent(date)) {
			jsonResponse = new Gson().toJson("error");
			return ERROR;
		}
		String description = ServletActionContext.getRequest().getParameter("description");
		Holiday holiday = new Holiday();
		holiday.setDate(date);
		holiday.setDescription(description);
		holidayDAO.add(holiday);
		logger.info("New Holiday - " + holiday.getDescription() + " Id - " + holiday.getId() + " - added Successfully");
		jsonResponse = new Gson().toJson("success");
		return SUCCESS;
	}

	private Date getDate() {
		Date date = null;
		try {
			date = sdf.parse(ServletActionContext.getRequest().getParameter("date"));
		}
		catch (ParseException e) {
			logger.info("Error while parsing date");
		}
		return date;
	}

	@Action(value = "initEditHolidayScreen", results = {
		@Result(name = "success", type = "json")
	}, interceptorRefs = {
		@InterceptorRef(value = "adminStack")
	})
	public String selectHoliday() throws Exception {
		Holiday holiday = holidayDAO.getHoliday(id);
		holiday.setId(id);
		try {
			jsonResponse = new Gson().toJson(holiday);
		}
		catch (Exception e) {
			logger.info(e.getMessage());
		}
		return SUCCESS;
	}

	@Action(value = "updateHoliday", results = {
	@Result(name = "success", type = "json"), @Result(name = "error", type = "json")
	}, interceptorRefs = {
		@InterceptorRef(value = "adminStack")
	})
	public String updateHoliday() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		Holiday holiday = new Holiday();
		Date date = getDate();
		if (holidayDAO.checkIfAlreadyPresent(date)) {
			return ERROR;
		}
		holiday.setDate(date);
		holiday.setId(id);
		holiday.setDescription(request.getParameter("description"));
		jsonResponse = new Gson().toJson(holiday);
		holidayDAO.update(holiday);
		logger.info("Holiday - " + holiday.getDescription() + " Id - " + holiday.getId() + " - updated Successfully");
		return SUCCESS;
	}

	public String clearSelectedHoliday() {
		return SUCCESS;
	}

	public String getJsonResponse() {
		return jsonResponse;
	}

	public void setJsonResponse(String jsonResponse) {
		this.jsonResponse = jsonResponse;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}
}
